package me.zhengjie.cron;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.ppService.ImitationTestService;
import me.zhengjie.ppService.PPMoveService;
import me.zhengjie.ppService.PPTaskResultService;
import me.zhengjie.pojo.sqlServer.PPMoveMission;
import me.zhengjie.pojo.sqlServer.PPTaskResult;
import me.zhengjie.modbusTCP.PalletTCP;
import me.zhengjie.wcs.PPWcsStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: xinglin
 * @CreateTime: 2024-08-21
 * @Description: pp移库任务定时执行
 * @Version: 1.0
 */



@Slf4j
@Component
@RequiredArgsConstructor
public class PPMovecCron {


    private  final PPWcsStatus ppWcsStatus;
    private  final PalletTCP palletTCP;
    private  final PPTaskResultService ppTaskResultService;
    private  final PPMoveService ppMoveService;
    private final ImitationTestService imitationTestService;

    private static final Logger ppLog = LoggerFactory.getLogger("pp");


    @Scheduled(fixedDelay = 3000)
    @Transactional
    public void StartInBoundTask(){
        if (!ppWcsStatus.allowSendTask()){
            ppLog.error("当前码垛机不能接收新任务");
        }else {
            // 查找有没有正在进行的移库任务
            PPTaskResult ppTaskResult = ppTaskResultService.findNowppTaskResult(3);
            // 查找最新的入库载盘任务
            PPMoveMission ppMoveMission = ppMoveService.findNewMoveMission();
            try {
                if (ObjectUtil.isEmpty(ppTaskResult) && ObjectUtil.isNotEmpty(ppMoveMission)) {
                    ppLog.info("查询到码垛机空闲，开始发送移库任务");
                    //向wcs发送移库任务
                    ppMoveService.sendMoveTask(ppMoveMission);
                    ppLog.info("发送移库任务成功，任务wms_task_id：{}", ppMoveMission.getMissionCode());
                }
            } catch (Exception e) {
                ppLog.error("移库任务发送到wcs失败，失败原因：{}", e.toString());
            }


        }

    }




}
